	package com.linchong.concurrency.lock;

	import com.linchong.concurrency.annoations.ThreadSafe;
	import lombok.extern.slf4j.Slf4j;

	import java.util.Map;
	import java.util.Set;
	import java.util.TreeMap;
	import java.util.concurrent.CountDownLatch;
	import java.util.concurrent.ExecutorService;
	import java.util.concurrent.Executors;
	import java.util.concurrent.Semaphore;
	import java.util.concurrent.locks.Lock;
	import java.util.concurrent.locks.ReentrantLock;
	import java.util.concurrent.locks.ReentrantReadWriteLock;

	/**
	 * @author linchong
	 * @version 1.0
	 * @Date: 2020-12-08 12:40
	 * @Description: ReentrantReadWriteLock$悲观读取，获取写锁的时候，保证没有任何的读写锁，如果读多写少，写线程可能会处于饥饿状态，即获取
	 * 不到锁，一直处于等待状态
	 */
	@Slf4j
	public class LockExample3 {

		private final Map<String,Data> map = new TreeMap<>();

		private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

		private final Lock readLock = lock.readLock();

		private final Lock writeLock = lock.writeLock();

		public Data get(String key){
			readLock.lock();
			try {
				return map.get(key);
			}finally {
				readLock.unlock();
			}
		}

		public Set<String> getAllKeys(){
			readLock.lock();
			try {
				return map.keySet();
			}finally {
				readLock.unlock();
			}
		}

		public Data put(String key,Data value){
			writeLock.lock();
			try {
				return map.put(key,value);
			}finally {
				writeLock.unlock();
			}

		}

		class Data{

		}
	}














